package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.user.convert.VersionControlConvert;
import com.yike.user.entity.VersionControl;
import com.yike.user.mapper.SoftVersionMapper;
import com.yike.user.param.VersionControlParam;
import com.yike.user.service.VersionControlService;
import com.yike.common.base.BaseServiceImpl;
import com.yike.user.vo.VersionControlVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 软件版本 服务实现类
 *
 * @author yike
 * @since 2024-07-25
 */
@Slf4j
@Service
public class VersionControlServiceImpl extends BaseServiceImpl<SoftVersionMapper, VersionControl> implements VersionControlService {

    @Resource
    private SoftVersionMapper softVersionMapper;

    @Override
    public VersionControlVo getLatest(VersionControlParam versionControlParam) {
        Wrapper wrapper = Wrappers.lambdaQuery(VersionControl.class)
                .eq(VersionControl::getSoftType, versionControlParam.getSoftType())
                .orderByDesc(VersionControl::getCsVersion)
                .last("limit 1");
        VersionControl versionControl = softVersionMapper.selectOne(wrapper);
        // 获取是否需要强制更新状态
        Integer maxForce = softVersionMapper.getMaxForce(versionControlParam.getSoftType(), versionControlParam.getCsVersion());
        versionControl.setForce(maxForce==null?0:maxForce);
        return VersionControlConvert.INSTANCE.convert(versionControl);
    }
}
